<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use think\Db;
// 导入对应产品模块的client
use TencentCloud\Live\V20180801\LiveClient;
// 导入要请求接口对应的Request类
use TencentCloud\Live\V20180801\Models\DescribeLiveStreamOnlineListRequest;
use TencentCloud\Live\V20180801\Models\DropLiveStreamRequest;
use TencentCloud\Live\V20180801\Models\ForbidLiveStreamRequest;
use TencentCloud\Live\V20180801\Models\ResumeLiveStreamRequest;

use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Credential;


class Wifi extends Common{
    /**
     * @desc wifi码列表
     * @return mixed
     * @date 2023-02-23 13:12
     */
    public function lst(){
        $limit = input('param.limit/d', 7);
        $keyword = input('param.keyword');
        $where=[];
        if ($keyword) {
            $where['s.shop_name'] = ['like', "%{$keyword}%"];
        }
        $field = 'a.*,s.shop_name';
        $list = Db::name('wifi')
            ->alias('a')
            ->field($field)
            ->join('shops s','s.id = a.shop_id','LEFT')
            ->where($where)
            ->order('a.id desc')
            ->paginate($limit)
            ->each(function ($item){
                $item['type_name'] =  db('industry')->where(['id'=>$item['type_id']])->value('industry_name');
                return $item;
            });
        $page = $list->render();
        $this->assign([
            'list'=>$list,
            'page'=>$page
        ]);
        return $this->fetch();
    }

    /**
     * @desc 批量生成wifi码
     * @return mixed|\think\response\Json
     * @date 2023-02-23 13:32
     */
    public function add(){
        if(request()->isAjax()){
            $admin_id = session('admin_id');
            $data = input('post.');
            $result = $this->validate($data,'Live');
            if(true !== $result){
                $value = array('status'=>0,'mess'=>$result);
            }else{
                $shop = db('shops')->find($data['shop_id']);
                if(!$shop){
                    $value = array('status'=>0,'mess'=>'该店铺ID不存在');
                    return json($value);
                }
                $data['create_time'] = time();
                $res = db('live')->insert($data);
                if($res){
                    $value = array('status'=>1,'mess'=>'添加成功');
                }else{
                    $value = array('status'=>0,'mess'=>'添加失败');
                }
            }
            return json($value);
        }else{
            $startnum = 0;
            return $this->fetch();
        }
    }
    /**
     * @desc 批量删除wifi码
     * @return \think\response\Json
     * @date 2023-02-23 13:31
     */
    public function delete() {

        if (input('post.id')) {
            $id = array_filter(explode(',', input('post.id')));
        } else {
            $id = input('id');
        }

        if (!empty($id)) {
            if (!is_array($id)) {
                $shop = Db::name('wifi')->where('id', $id)->where('is_recycle', 1)->find();
                if ($shop) {
                    // 启动事务
                    Db::startTrans();
                    try {
                        $delResult = db('live')->where('id', $id)->delete();
                        // 提交事务
                        Db::commit();
                        ys_admin_logs('删除直播间成功', 'live', $id);
                        $value = array('status' => 1, 'mess' => '删除直播间成功');
                    } catch (\Exception $e) {
                        // 回滚事务
                        Db::rollback();
                        $value = array('status' => 0, 'mess' => '删除直播间失败');
                    }
                } else {
                    $value = array('status' => 0, 'mess' => '找不到相关信息');
                }
            } else {
                $idarr = $id;
                foreach ($idarr as $vd) {
                    $shop = Db::name('wifi')->where('id', $vd)->where('is_recycle', 1)->find();
                    if ($shop) {
                        // 启动事务
                        Db::startTrans();
                        try {
                            $delResult = db('live')->where('id', $vd)->delete();
                            // 提交事务
                            Db::commit();
                            ys_admin_logs('删除直播间成功', 'live', $vd);
                        } catch (\Exception $e) {
                            // 回滚事务
                            Db::rollback();
                        }
                    }
                }
                $value = array('status' => 1, 'mess' => '删除直播间成功');
            }
        } else {
            $value = array('status' => 0, 'mess' => '未选中任何数据');
        }
        return json($value);
    }

    //清除wifi缓存
    public function cleancache(){
        $id = input('id');
        if (!empty($id) && !is_array($id)) {
            $shop = Db::name('wifi')->where('id', $id)->where('is_recycle', 0)->find();
            if ($shop) {
                // 启动事务
                Db::startTrans();
                try {
                    Db::name('wifi')->where('id', $id)->update(array('is_recycle'=>1));

                    // 提交事务
                    Db::commit();
                    $value = array('status' => 1, 'mess' => '加入回收站成功');

                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    $value = array('status' => 0, 'mess' => '加入回收站失败');
                }
            } else {
                $value = array('status' => 0, 'mess' => '找不到相关信息');
            }
        } else {
            $value = array('status' => 0, 'mess' => '未选中任何数据');
        }
        return json($value);
    }

    /**
     * @desc wifi码模板
     * @date 2023-02-23 13:33
     */
    function addstyle(){

        return $this->fetch();
    }

    /**
     * @desc wifi模板列表
     * @return mixed
     * @date 2023-02-23 14:08
     */
    function stylelst(){
        $list = Db::name('wifi_style')->paginate(25);
        $page = $list->render();

        if(input('page')){
            $pnum = input('page');
        }else{
            $pnum = 1;
        }
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('pnum',$pnum);

        return $this->fetch();
    }

}